package it.univaq.mancoosi.injectors.systemconfiguration.managers;


import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu.UbuntuEnvironmentManager;
import it.univaq.mancoosi.mancoosimm.MancoosiFactory;
import it.univaq.mancoosi.mancoosimm.MenuEntry;
import it.univaq.mancoosi.mancoosimm.MenuSection;
import it.univaq.mancoosi.mancoosimm.MimeType;
import it.univaq.mancoosi.mancoosimm.MimeTypeHandler;
import it.univaq.mancoosi.mancoosimm.MimeTypeHandlerCache;


public abstract class MimeTypeHandlerCacheManager {
	   protected static MimeTypeHandlerCacheManager INSTANCE ;	   
	   protected MimeTypeHandlerCache mimeTypeHandlerCache;
	   protected HashMap<String, MimeTypeHandler> mimeTypeHandlerMap;
	   protected HashMap<String, MimeType> mimeTypeMap;
	   protected MancoosiFactory factory;

	   
	   // Private constructor prevents instantiation from other classes
	   protected MimeTypeHandlerCacheManager() {
		   factory = MancoosiFactory.eINSTANCE; 
		   mimeTypeHandlerCache = factory.createMimeTypeHandlerCache();
		   mimeTypeHandlerMap = new HashMap<String, MimeTypeHandler>();
		   mimeTypeMap = new HashMap<String, MimeType>();
	   }
	 
	   public static MimeTypeHandlerCacheManager getInstance() {
	      return INSTANCE;
	   }
	   
	   public MimeTypeHandlerCache getMimeTypeHandlerCache() {
		   return mimeTypeHandlerCache;
	   }
	   
	   public void syncMimeTypeHandlerMap () {
			Iterator<MimeTypeHandler> iterator  = null;
			if(UbuntuEnvironmentManager.getInstance().getEnvironment().getMimeTypeHandlerCache() != null){
				iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getMimeTypeHandlerCache().getHandlers().iterator();
			}
			if(iterator != null){
				while (iterator.hasNext()) {
					MimeTypeHandler curr = iterator.next();
					this.mimeTypeHandlerMap.put(curr.getType().getName(), curr);
				}
			}
		}
		
		public void syncMimeTypeMap () {
			Iterator<MimeType> iterator  = null;
			if(UbuntuEnvironmentManager.getInstance().getEnvironment().getMimeTypeHandlerCache() != null){
				iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getMimeTypeHandlerCache().getMimeTypes().iterator();
			}
			if(iterator != null){
				while (iterator.hasNext()) {
					MimeType curr = iterator.next();
					this.mimeTypeMap.put(curr.getName(), curr);
				}
			}
		}
		
		public abstract Boolean existsMimeTypeHandler(String mimeTypeName);
		
		public abstract Boolean existsMimeType(String mimeTypeName);
	   
		public abstract void createMimeTypeHandlerCacheFromSystem();
		   
		public abstract void synchronize() throws IOException;
		


}
